const mongoose = require("mongoose");



// 获取排片数据
const get = async ({ curPage, eachPage }) => {
    // 转换类型
    (curPage = ~~curPage), (eachPage = ~~eachPage);
    const data = {
        total: 0,
    }
    // 获取model
    const schedulesModel = mongoose.model("schedules");
    // 获取当前model映射的那个集合里存在的文档总数
    data.total = await schedulesModel.countDocuments();
    // 获取总页数
    data.maxPage = Math.ceil(data.total / eachPage);
    const skipNum = (curPage - 1) * eachPage;
    data.rows = await schedulesModel.find().populate("cinemasId").populate("movieId").populate("theaterId").skip(skipNum).limit(eachPage).sort({ _id: -1 }).exec();
    console.log(data);
    return data;
}

// 获取电影
const getMovie = async () => {
    const moviesModel = mongoose.model("movies");
    return await moviesModel.find();
}

// 获取影院
const getCinemas = async () => {
    const cinemasModel = mongoose.model("cinemas");
    return await cinemasModel.find();
}
// 获取放映厅
const getTheaters = async ({ _id }) => {
    const theatersModel = mongoose.model("theaters");
    return await theatersModel.find({ cinemasId: _id }).sort({ _id: 0 });
}

// 新增排片
const addSchedules = async ({ movieId, cinemasId, theaterId, showTime, price }) => {
    const schedulesModel = mongoose.model("schedules");
    // 1、新增排片信息
    const { _id: schedulesId } = await schedulesModel.create({ movieId, cinemasId, theaterId, showTime, price });
    // 2、获取排片信息中，涉及的放映厅所对应的座位信息
    const seatsArr = await mongoose.model("seats").find({ theaterId }).sort({ _id: 0 });
    // 3、创建属于当前排片信息的座位数据
    const seatingsArr = seatsArr.map(item => ({
        seatId: item._id,
        schedulesId,
        status: 0,
    }))
    // 4、把座位信息加入数据库
    await mongoose.model("seatings").create(seatingsArr);
}
// 删除排片
const removeSchedules = async ({ _id }) => {
    // 获取model
    const schedulesModel = mongoose.model("schedules");
    const data = await schedulesModel.remove({ _id });
    // 删除排片_id拥有的选座集合
    await mongoose.model("seatings").remove({ schedulesId: _id });
    // console.log(data);
}

// // 获取排片的座位
const getSeatings = async schedulesId => {
    const seatingsModel = mongoose.model("seatings");
    return await seatingsModel.find({ schedulesId }).populate("seatId").populate("schedulesId").sort({ _id: 0 }).exec();
}

// 通过排片id找到整条数据
const sortmoviesData = async _id => {
    const schedulesModel = mongoose.model("schedules");
    return await schedulesModel.find({ _id }).populate("cinemasId").populate("movieId").populate("theaterId").exec();
}


// // 新增订单
const addOrder = async (seatId, userId) => {
    const seatingsModel = mongoose.model("seatings");
    const orderModel = mongoose.model("order");
    // 通过循环修改选座的状态
    for (let i = 0; i < seatId.length; i++) {
        await seatingsModel.updateOne({ _id: seatId[i].seatId }, { status: 1 });
    };
    // 找到座位对应的排片ID
    const { schedulesId } = await seatingsModel.findOne({ _id: seatId[0].seatId });
    // 通过遍历的方式将整合订单信息
    let data = [];
    seatId.map(item => {
        data.push({ schedulesId, seatingsId: item.seatId, userId });
    })
    const result = await orderModel.create(data);
    if (result.length == seatId.length) {
        return { status: true };
    } else {
        return { status: false };
    }
}

// 查询电影
const findMovie = async movieId => {
    const schedulesModel = mongoose.model("schedules");
    return await schedulesModel.find({ movieId }).populate("cinemasId").populate("movieId").populate("theaterId").sort({ _id: -1 }).exec();
}


module.exports = {
    get,
    getMovie,
    getCinemas,
    getTheaters,
    addSchedules,
    removeSchedules,
    getSeatings,
    addOrder,
    sortmoviesData,
    findMovie,

};